//
//  ShuffView.m
//  Time
//
//  Created by dllo on 16/7/22.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "ShuffView.h"
#import "SCBNetTool.h"
#import "UIImageView+AFNetworking.h"

@interface ShuffView ()
<
UICollectionViewDataSource,
UICollectionViewDelegate
>

@property (nonatomic,retain) UICollectionView *collection;
@property (nonatomic,retain) NSMutableArray *dicArray;

@end

@implementation ShuffView
-(void)dealloc{
    [_dicArray release];
    _collection.delegate = nil;
    _collection.dataSource = nil;
    [_collection release];
    [super dealloc];
}


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.dicArray = [NSMutableArray arrayWithCapacity:5];
        [self AFHandleWork];
        
        
        [self createCollection];
    }
    return self;
}

-(void)AFHandleWork{
    
    //url
    NSString *urlStr = @"http://api.pianke.me/version4.0/pub/ad.php?sig=00C17A37F4F3693CF0A8140A7A45DC03";
    NSString *body = @"type=2";
    
    [SCBNetTool POST:urlStr withBody:body withBodyStyle:SCBBodyString withHeaderFile:@{@"Authorization":HEADER} withResponse:SCBJSON withSuccess:^(id result) {
        //        NSLog(@"%@",result);
        NSDictionary *dictionary = result;
        NSArray *array = [dictionary valueForKey:@"data"];
        
        self.dicArray = [NSMutableArray arrayWithArray:array];
        [self.dicArray insertObject:[array lastObject] atIndex:0];
        [self.dicArray addObject:[array firstObject]];
        [self.collection reloadData];
        self.collection.contentOffset = CGPointMake(SWIDTH , 0);
        
    } withFaolure:^(NSError *error) {
        NSLog(@"轮播图数据抓取失败,%@",error);
    }];
    
}

-(void)createCollection{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(SWIDTH, self.bounds.size.height);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) collectionViewLayout:flowLayout];
    self.collection.showsHorizontalScrollIndicator = NO;
    self.collection.delegate = self;
    self.collection.dataSource = self;
    self.collection.pagingEnabled = YES;
    [self addSubview:self.collection];
    
    [self.collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"SCBShuffCell"];
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(changeOffset) userInfo:nil repeats:YES];
    
    [flowLayout release];
    [self.collection release];
}
-(void)changeOffset{
    [self.collection setContentOffset:CGPointMake(self.collection.contentOffset.x + SWIDTH, 0) animated:YES];
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dicArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SCBShuffCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:arc4random() % 255 / 255.0 green:arc4random() % 255 / 255.0 blue:arc4random() % 255 / 255.0 alpha:1];
    UIImageView *imageView = [[UIImageView alloc] init];
    NSDictionary *dic = [self.dicArray objectAtIndex:indexPath.item];
    NSString *imgUrl = [dic valueForKey:@"img"];
    NSURL *url = [NSURL URLWithString:imgUrl];
    [imageView setImageWithURL:url placeholderImage:nil];
    cell.backgroundView = imageView;
    return cell;
}



-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x == 0) {
        scrollView.contentOffset = CGPointMake(SWIDTH * (self.dicArray.count - 2), 0);
    }
    if (scrollView.contentOffset.x >= (self.dicArray.count - 1) * SWIDTH) {
        scrollView.contentOffset = CGPointMake(SWIDTH, 0);
    }
}



@end
